Load data and estimates

Fix the variable names if not done

Add the parameter estimates to the tibble

Set up the experiment-specific parameters

numItemsInStream<- length( dg$letterSeq[[1]] )  
minSPE<- -17; maxSPE<- 17
annotateIt<-TRUE

numSs<-length(unique(dg$subject))
cat( paste0('Total num Ss=',numSs) )
## Total num Ss=79

Calc numObservations for each condition

#Calc numObservations for each condition. This is needed  for scaling the fine-grained Gaussian
#gaussianScaledforData needs to know.
if (!("nPerCond" %in% colnames(dg))) {
  dfGroups<-  dg  %>%  filter( !is.na(targetSP) ) %>% group_by_at(.vars = condtnVariableNames) %>% summarise(nPerCond = n())
  #add nPerCond back to parameter estimates
  estimates<- merge(estimates,dfGroups)
}

Calculate curves for parameters, to plot on histograms.

#devtools::install_github('alexholcombe/mixRSVP',build_vignettes=TRUE)
library(mixRSVP)
oneTarget<- dg %>% filter(targetSide!="Both")
curvesOneTarget<- dg %>% filter(targetSide!="Both") %>%
  group_by_at(.vars = condtnVariableNames) %>% 
  do(calc_curves_dataframe(.,minSPE,maxSPE,numItemsInStream))
#fails with on-missing arguments to min; returning Inf no non-missing arguments to max; returning -Inf 
#Error in createGuessingDistribution(minSPE, maxSPE, df$targetSP, numItemsInStream) :  minSPE must be less than or equal to maxSPE

#DEBUG
# dd<- dg %>% filter(subject=="AA", stream=="Left",targetSide=="Both",orientation=="Canonical")
# 
# createGuessingDistribution(minSPE,maxSPE,dd$targetSP,numItemsInStream)
# targetSP<- dd$targetSP
# maxTargetSP <- max(targetSP)
# minSPEthisData<- 1 - max(targetSP)
# maxSPEthisData<- numItemsInStream - min(targetSP)
# if (maxSPEthisData > maxSPE) stop("maxSPE must be at least",maxSPEthisData,"based on the values you passed me")
# if (minSPEthisData < minSPE) stop("minSPE must be no greater than",minSPEthisData,"based on the values you passed me")
# calc_curves_dataframe(dd,minSPE,maxSPE,numItemsInStream)
# #Calc numObservations to each condition. This is needed only for scaling the fine-grained Gaussian
# #Calc the number of observations for each condition, because gaussianScaledforData needs to know.
# dfGroups<- dg %>% group_by_at(.vars = condtnVariableNames) %>% summarise(nPerCond = n())
# #add nPerCond back to parameter estimates
# estimates<- merge(estimates,dfGroups)

grain<-.05
gaussFine<- estimates %>%  filter(targetSide!="Both") %>% group_by_at(.vars = condtnVariableNames) %>% do(
  gaussian_scaled_from_df(.,minSPE,maxSPE,grain) )

Plot sample histograms, make sure plotting working.

library(ggplot2)

dAC<- dplyr::filter(oneTarget,subject=="AC")  
plotContinuousGaussian<- TRUE 
annotateIt<-TRUE
g<- plot_hist_with_fit(dAC,minSPE,maxSPE,d915$targetSP,numItemsInStream,
                        plotContinuousGaussian,annotateIt, FALSE)
g <- g + geom_vline(xintercept = 0)
g + annotate("text", x = 12, y = 25, label = "AC")

Show single plot with curves using scalable method to facet_grid. In other words, make sure can plot without plot_hist_with_fit

## Scale for 'x' is already present. Adding another scale for 'x', which
## will replace the existing scale.

Plot all Ss

g=ggplot(oneTarget, aes(x=SPE)) + 
        facet_grid(subject+orientation~stream) #,  scales="free_y")
g<-g+geom_histogram(binwidth=1,color="grey90") + xlim(minSPE,maxSPE)
g<-g+ geom_text(x=12, y= 33, aes(label = subject)) #inset subject name/number. Unfortunately it overwrites itself a million times
g<-g +theme_apa() #+theme(panel.grid.minor=element_blank(),panel.grid.major=element_blank())# hide all gridlines.
g<-g +xlim(minSPE,maxSPE)
## Scale for 'x' is already present. Adding another scale for 'x', which
## will replace the existing scale.
sz=.3
#show(g)
g<-g+ geom_point(data=curvesOneTarget,aes(x=x,y=combinedFitFreq),color="chartreuse3",size=sz)
#g<-g+ geom_line(data=curves,aes(x=x,y=guessingFreq),color="yellow",size=sz)
#Discretized Gaussian
#g<-g+ geom_line(data=curves,aes(x=x,y=gaussianFreq),color="lightblue",size=sz)

#mixSig - whether mixture model statistically significantly better than guessing
curvesOneTarget <- dplyr::mutate(curvesOneTarget, mixSig = ifelse(pLRtest <= .05, TRUE, FALSE)) #annotate_fit uses this to color the p-value
g<- annotate_fit(g,curvesOneTarget) #assumes curvesDf includes efficacy,latency,precision
#Somehow the which mixSig (TRUE or FALSE) is red and which green is flipped relative to plot_hist_with_fit even though
#identical commands are used. I haven't been able to work out why.
#g<- g + scale_color_manual(values=c("red","forestgreen")) #already done in annotate_fit
show(g)

#SsWhoLookWeird<-c("CY","AP","BJ")
# CY is bimodal but not outrageously so.
# 
# Subject AP looks to both have close to zero efficacy in some conditions and disregards instructions, reporting mostly after the cue.
# 
# BJ has essentially zero efficacy and may have tended to report letters from beginning of stream in one condition.

#AP, BJ does flunk likelihood test! CY passes but data not horrifyingly bad.
#Exclude by likelihood ratio test

Participant AI, BB suspicion of attending to one side from twoTargets condition.